/*
 *  Netzpult - Your desk on the web
 *  Copyright (C) 2011  Ulrich Hilger, http://uhilger.de
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/
 */

package de.uhilger.netzpult.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * 
 * @author Copyright (c) Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of
 * the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
 */
public class Netzpult implements EntryPoint {

	//private static final String UI = "ui";
	//private static final String DOC_ID = "id";
	private static final String TEXTER_UI = "texter";
	//private static final String PUBLIC_UI = "public";
	private static final String EXPLORER_UI = "ablage";
	private static final String PAGE_UI = "seite";
	
	private Seite ui;

	private final OwnerServiceAsync ownerService = GWT.create(OwnerService.class);
  
	public void onModuleLoad() {
		RootLayoutPanel rootPanel = RootLayoutPanel.get();
		String bereich = getBereich();
		if(TEXTER_UI.equalsIgnoreCase(bereich)) {
			Texter texter = new Texter(ownerService, rootPanel.getOffsetHeight());
			rootPanel.add(texter);
			texter.getEditor().toggleEditor();
			String docId = getDocId();;
			if(docId != null) {
				texter.openDocument(docId);
			}
		} else if(PAGE_UI.equalsIgnoreCase(bereich)) {
			openDefaultUi(rootPanel);
		} else if(EXPLORER_UI.equalsIgnoreCase(bereich)){
			Ablage explorer = new Ablage(ownerService);
			rootPanel.add(explorer);
		} else {
			openDefaultUi(rootPanel);
		}
	}
	
	public static String getBereich() {
		//String[] path = Window.Location.getPath().split("/");
		/*for(int i=0; i < path.length; i++) {
			Window.alert("path[" + i + "]=" + path[i]);
		}*/

		/*
		String bereich = null;
		if(path.length > 1) {
			bereich = path[1];
		} else {
			bereich = path[0];
		}
		*/
		
		String bereich = "seite";
		String path = Window.Location.getPath();
		if(path != null) {
			path = path.toLowerCase();
			if(path.contains(EXPLORER_UI)) {
				bereich = EXPLORER_UI;
			} else if(path.contains(TEXTER_UI)) {
				bereich = TEXTER_UI;
			}
		}
		return bereich;
	}
	
	public static String getDocId() {
		String[] path = Window.Location.getPath().split("/");
		String id = null;
		if(path.length > 2) {
			id = path[path.length - 1];
		}
		return id;
	}
	
	private void openDefaultUi(final RootLayoutPanel rootPanel) {
		//ui = new Seite();
		
		//rootPanel.add(ui);
		
		ui = new Seite(new AsyncListener(){

			@Override
			public void isDone() {
				rootPanel.add(ui);
				
			}});
	}	
}
